package com.kalman.biz.taobao.spider;

import static com.google.common.base.Strings.isNullOrEmpty;

import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.kalman.biz.taobao.domain.TbShop;
import com.kalman.biz.taobao.open.ITaobaoShopService;
import com.kalman.common.spider.AbstractWebSpiderListener;
import com.kalman.common.spider.WebPage;
import com.kalman.common.spider.WebSpiderParam;
import com.kalman.dao.pojo.TaobaoCouponsDO;
import com.kalman.dao.taobao.ITaobaoCouponsDao;

/**
 * @author kalman03
 */
public class TaobaoWebSpiderListener extends AbstractWebSpiderListener {

	private final static Logger log = LoggerFactory.getLogger(TaobaoWebSpiderListener.class);
	/**匹配的正则表达式*/
	private String [] regex;
	@Autowired
	private ITaobaoCouponsDao couponsDao;
	@Autowired
	private ITaobaoShopService taobaoShopService;

	@Override
	public void onStarted(WebSpiderParam param) {
		log.warn("TaobaoWebSpiderListener onStarted.param is "+ param.toString());
	}

	@Override
	public void onException(Throwable t) {
		log.error("TaobaoWebSpiderListener Catch a exception.", t);
	}

	@Override
	public void handleWebPage(WebPage webPage) {
		if (webPage.hasHttpLinks()) {
			for (String link : webPage.getMatchedLinkList(regex)) {
				System.out.println(link);
				doInsertDB(link);
			}
		}
	}

	@Override
	public void onEnd(long success, long failed) {
		long count = success + failed;
		log.warn("TaobaoWebSpiderListener onEnd,爬取页面结束，其中爬到页面" + count + "张，成功" + success + "张，失败"+ failed + "张");
	}

	private void doInsertDB(String link) {
		try {
			link = formatLink(link);
			String nick = getUserNick(link);
			TbShop shop = taobaoShopService.getShop(nick);
			
			TaobaoCouponsDO coupon = new TaobaoCouponsDO();
			coupon.setLink(link);
			coupon.setNick(nick);
			coupon.setShopLogo(shop.getLogo());
			coupon.setShopUrl(shop.getUrl());
			
			couponsDao.addCoupon(coupon);
			log.warn(String.format("爬取到的链接:%s",coupon.getLink()));
		} catch (Exception e) {
			log.error("catch a exception.",e);
		}
	}
	
	private String formatLink(String link){
		return link.replaceAll("&amp;", "&");
	}

	public void setRegex(String[] regex) {
		this.regex = regex;
	}
	
	private String getUserNick(String link){
		long userId = LinkHelper.getUserId(link);
		return NickHelper.getNick(userId);
	}
	
	static class LinkHelper{
		public static long getUserId(String link){
			long userId = -1;
			if(! isNullOrEmpty(link)){
				Pattern pattern = Pattern.compile("sellerId=[0-9]{1,50}"); 
				Matcher matcher = pattern.matcher(link); 
				while(matcher.find()) { 
					String str = matcher.group();
					str = str.replaceAll("sellerId=", "");
					userId = Long.parseLong(str);
				} 
			}
			return userId;
		}
		
		public static void main(String[] args) {
			long id = LinkHelper.getUserId("http://taoquan.taobao.com/coupon/shopbonus/buyer_apply.htm?spm=a1z10.1.w18247596152.4.NQtIFF&activityId=30432747&sellerId=468264976&s");
			System.out.println(id);
		}
	}
	
	static class NickHelper {
		/**
		 * 根据id爬取nick
		 * @param userId
		 */
		public static String getNick(long userId){
			String result = "";
			try {
				URL url = new URL("http://i.taobao.com/"+userId+"/photo/album.htm");
				Parser parser = new Parser(url.openConnection());
				
				NodeList nodeList = null;
				nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {
					private static final long serialVersionUID = 1L;
					// 实现该方法,用以过滤标签
					public boolean accept(Node node) {
						if (node instanceof LinkTag) // <A>标记
							return true;
						return false;
					}
				});

				for (int i = 0; i < nodeList.size(); i++) {
					LinkTag tag = (LinkTag) nodeList.elementAt(i);
					String nick = tag.getAttribute("data-nick");
					if(!isNullOrEmpty(nick) && "J_sendMes".equals(tag.getAttribute("id"))){
						result = nick;
						break;
					}
				}
			} catch (Exception e) {
				Throwables.propagate(e);
			}
			return result;
		}
		
		public static void main(String[] args) {
			String nick = getNick(431082987);
			System.out.println(nick);
		}
	}
}
